package cn.edu.hpu.jie.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import cn.edu.hpu.jie.entity.Rule;
import cn.edu.hpu.jie.util.RowMapping;
import cn.edu.hpu.jie.util.VoteDB;

public class RuleDao {
	private VoteDB<Rule> db = new VoteDB<Rule>();
	
	private class RuleMapping implements RowMapping<Rule>{

		public Rule getMapping(ResultSet rs) throws SQLException {
			Rule rule = Rule.getRule();
			rule.setId(rs.getInt("id"));
			rule.setBegintime(rs.getString("begintime"));
			rule.setEndtime(rs.getString("endtime"));
			rule.setMincheck(rs.getInt("mincheck"));
			rule.setMaxcheck(rs.getInt("maxcheck"));
			return rule;
		}
	}
	
	/**
	 * 更新rule记录
	 * @param r
	 * @return
	 */
	public int updateRule(Rule r) {
		String sql = "UPDATE t_rule SET begintime = ?, endtime = ?, mincheck = ?, maxcheck = ? WHERE id = ?";
		return db.updateData(sql, r.getBegintime(), r.getEndtime(), r.getMincheck(), r.getMaxcheck(), r.getId());
	}
	
	/**
	 * 获取记录
	 * @return
	 */
	public Rule getRule() {
		String sql = "SELECT id, begintime, endtime, mincheck, maxcheck FROM t_rule";
		return db.getObject(sql, new RuleMapping());
	}
}
